import { mapGetters } from "vuex";

const baseMixin = {
  data() {
    return {
        permissionStatus: false,
        saveStatus: false,
        deleteStatus: false,
        newStatus: false,
        listViewStatus: false,
        addStatus: false,
        deleteRoleStatus: false,
    }
  },
  computed: {
    ...mapGetters(['btnCodeList']),
    btnPermNameList(){
      return this.btnCodeList
    }
  },
  methods: {
    
    getOptions(url) {
      let optionsObject = this.$store.state.optionsObject;
      if (optionsObject[url]) {
        return optionsObject[url];
      } else {
        let promise = this.selectAjax(url);
        optionsObject[url] = [];
        promise.then(res => {
          if (res.length > 0) {
            res.forEach(item => {
              optionsObject[url].push(item);
            });
            this.$store.commit("setOptionsObject", optionsObject);
          }
        });
        return optionsObject[url];
      }

    },
    selectAjax: function(url) {
      return new Promise((resolve, reject) => {
        this.$api.get(url).then(res => {
            if (res.data.list) {
              resolve(res.data.list);
            } else {
              resolve(res.data);
            }
        }).catch(error => {
          reject(error);
        });
      });
    },
    //去空
    toEmpty(data) {
      let newData = {};
      for (let i in data) {
        if (data[i] != "" || typeof data[i] == "number") {
          newData[i] = data[i];
        }
      }
      return newData;
    },
    crtTimeFtt(value) {
      if (!value) return;
      let crtTime = new Date(value);
      // return dateFtt("yyyy-MM-dd hh:mm:ss", crtTime); //直接调用公共JS里面的时间类处理的办法
      let fmt = "yyyy-MM-dd hh:mm:ss";
      let date = crtTime;
      let o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
      return fmt;
    }
  },
};
export default baseMixin;
